package com.ddatsh.transaction.core.model;


import java.util.Properties;

import com.ddatsh.transaction.core.exception.TransException;
import com.ddatsh.transaction.core.lifecycle.processunit.ProcessUnit;
 
/**
 * 交易上下文
 * 
 * @author ddatsh
 * @version 1.0
 */
public class TransContext implements java.io.Serializable {
	private static final long serialVersionUID = 3847889406639504739L;
	private Request request;
	private Response response;
	private Transaction transaction;
	private TransactionDefinition transDef;
	private Properties applicationProperies;
	private ProcessUnit[] transProcessUnits;
	private Object[] processResults;
	private TransException exception;

	/**
	 * Creates a new TransContext object. 构造方法
	 * 
	 * @param request
	 *            请求包
	 * @param transDef
	 *            本交易定义
	 * @param applicationProperties
	 *            应用配置属性
	 * @param transProcessUnits
	 *            本交易处理单元
	 */
	public TransContext(final Request request, final TransactionDefinition transDef, final Properties applicationProperties,
			ProcessUnit[] transProcessUnits) {
		super();
		this.applicationProperies = applicationProperties;
		this.transaction = new Transaction(request.getRequestNo());
		this.request = request;
		this.transDef = transDef;
		this.transProcessUnits = transProcessUnits;
		if (transProcessUnits != null) {
			this.processResults = new Object[transProcessUnits.length];
		}
	}

	/**
	 * 获取交易体
	 * 
	 * @return 交易体
	 */
	public Transaction getTransaction() {
		return transaction;
	}

	/**
	 * 获取交易定义
	 * 
	 * @return 本交易定义
	 */
	public TransactionDefinition getTransDef() {
		return transDef;
	}

	/**
	 * 获取应用程序属性（从应用上下文克隆出来的）
	 * 
	 * @return 应用程序属性
	 */
	public final Properties getApplicationProperies() {
		return applicationProperies;
	}

	/**
	 * 应用上下文值
	 * 
	 * @param key
	 *            key
	 * 
	 * @return value
	 */
	public final String getAppContextValue(String key) {
		return this.applicationProperies.getProperty(key);
	}

	/**
	 * 获取执行期异常
	 * 
	 * @return 异常信息
	 */
	public TransException getException() {
		return exception;
	}

	/**
	 * 异常信息
	 * 
	 * @param exception
	 *            异常
	 */
	public void setException(TransException exception) {
		this.exception = exception;
	}

	/**
	 * 获取本交易需要处理的执行单元
	 * 
	 * @return 执行单元
	 */
	public ProcessUnit[] getTransProcessUnits() {
		return transProcessUnits;
	}

	/**
	 * 处理结果
	 * 
	 * @return 交易处理结果
	 */
	public Object getProcessResult(String processUnitName) {
		for (int i = this.transProcessUnits.length - 1; i >= 0; i--) {
			if (this.transProcessUnits[i].getName().equals(processUnitName)) {
				return processResults[i];
			}
		}

		return null;
	}

	/**
	 * 添加处理结果到上下文
	 * 
	 * @param transProcessUnitName
	 *            交易处理器名称
	 * @param processResult
	 *            处理结果
	 */
	public void addProcessResults(String transProcessUnitName, Object processResult) {
		for (int i = 0; i < this.transProcessUnits.length; i++) {
			if (this.transProcessUnits[i].getName().equals(transProcessUnitName)) {
				this.processResults[i] = processResult;
			}
		}
	}

	/**
	 * 获取请求对象
	 * 
	 * @return 请求对象
	 */
	public Request getRequest() {
		return request;
	}

	public Response getResponse() {
		return response;
	}

	public void setResponse(Response response) {
		this.response = response;
	}

}
